﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.IBizLogic;
using Yunwei.Web.Areas.RRShop.Models;

namespace Yunwei.Web.Areas.RRShop.Controllers
{
    public class CategoryController : Controller
    {
        ICategoryService _categoryService;
        public CategoryController()
        {
            _categoryService = new CategoryService();
        }
        // GET: RRShop/Category
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult List(int id)
        {
            ViewBag.Id = id;

            var categorys = _categoryService.FindList(10, "", id, 1);
            List<CategoryViewModel> cvms = new List<CategoryViewModel>();
            foreach(var c in categorys)
            {
                cvms.Add(CategoryViewModel.FromCategory(c));
            }
            return View(cvms);
        }

        [Authorize]
        public ActionResult Add(int id)
        {
            ViewBag.Id = id;
            return View(new CategoryViewModel() { Restaurant_Id = id });
        }

        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Add(CategoryViewModel cvm, int id)
        {
            if(ModelState.IsValid)
            {
                var category = _categoryService.Add(cvm.Category);
                if(category==null||category.Id<=0)
                {
                    ModelState.AddModelError("", "添加失败");
                    return View(cvm);
                }
                else
                {
                    return RedirectToAction("List", new { Id = cvm.Restaurant_Id });
                }
            }

            return View(cvm);
        }


        [Authorize]
        public ActionResult Edit(int id, int restaurant_id)
        {
            ViewBag.Id = restaurant_id;
            var category = _categoryService.Find(id);
            if (category == null)
            {
                return RedirectToAction("Error404", "Home", new { Area = "Admin", returnUrl = "../../RRShop/Category/List/" + restaurant_id });
            }

            return View(CategoryViewModel.FromCategory(category));
        }


        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(CategoryViewModel cvm, int id)
        {
            ViewBag.Id = cvm.Restaurant_Id;
            if (ModelState.IsValid)
            {
                if (_categoryService.Update(cvm.Category))
                    ModelState.AddModelError("", "修改成功");
                else
                    ModelState.AddModelError("", "修改失败");
            }
            return View(cvm);
        }


        [Authorize]
        public ActionResult Delete(int id, int restaurant_id)
        {
            var category = _categoryService.Find(id);
            _categoryService.Delete(category);

            return RedirectToAction("List", new { Id = restaurant_id });
        }
    }
}